const path = require('path');
const webpack = require('webpack');
const FileManagerPlugin = require('filemanager-webpack-plugin');
const Version = new Date().getTime();
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
module.exports = {
    // 修改 src 为 examples
    pages: {
        index: {
            entry: 'examples/main.js',
            template: 'public/index.html',
            filename: 'index.html'
        }
    },
    publicPath: '/aveh/page/',
    outputDir: 'dist',
    assetsDir: 'static',
    productionSourceMap: false,
    lintOnSave: false,
    runtimeCompiler: true,
    filenameHashing: false,
    devServer: {
        port: 9902, //固定端口
        hot: true, //开启热更新
        disableHostCheck: true
    },
    // 扩展 webpack 配置，使 packages 加入编译
    chainWebpack: config => {
        config.resolve.alias.set('@', path.resolve('examples')).set('~', path.resolve('packages'));
        config.module
            .rule('js')
            .include.add(/packages/)
            .end()
            .include.add(/examples/)
            .end()
            .use('babel')
            .loader('babel-loader')
            .options({
                presets: [
                    [
                        '@babel/preset-env',
                        {
                            modules: false
                        }
                    ]
                ]
            });
        if (process.env.NODE_ENV === 'production') {
            // js文件包分析
            if (process.env.npm_config_report) {
                config
                    .plugin('webpack-bundle-analyzer')
                    .use(BundleAnalyzerPlugin)
                    .end();
            }
        }
        // 移除 prefetch 插件
        /*config.plugins.delete('prefetch-index');
        config.plugins.delete('preload-index');*/
        config.output.filename(`js/[name].${Version}.js`).chunkFilename(`js/[name].${Version}.js`);

        //config.entry.app = ['@babel/polyfill', './examples/main.js'];
    },
    configureWebpack: config => {
        config.externals = {
            vue: "Vue"
        };
        // webpack的相关配置在这里
        if (process.env.NODE_ENV === 'production') {
            config.plugins.push(
                new FileManagerPlugin({
                    // 初始化 filemanager-webpack-plugin 插件实例
                    onEnd: {
                        mkdir: ['./dist'],
                        delete: [
                            // 首先需要删除项目根目录下的dist.zip
                            './dist/dist.zip'
                        ],
                        archive: [
                            // 然后我们选择dist文件夹将之打包成dist.zip并放在根目录
                            { source: './dist', destination: './dist/dist.zip' }
                        ]
                    }
                })
            );
        }
    },
    // 全局less变量
    pluginOptions: {
        'style-resources-loader': {
            preProcessor: 'less',
            patterns: [path.resolve(__dirname, './packages/styles/theme.less')]
        }
    }
};
